package com.tlvip.arithmetic.ch08;

import java.io.*;
import java.util.Random;

/**
 * <p>description：200万学生成绩排序，学生分数0-900之间</p>
 * <p>copyright： copyright(C)2016-2099</p>
 * <p>Life is short,we need passion</p>
 * <p>Summary： </p>
 * <p>instructions： </p>
 * Date 1/27/21</p>
 * Author mac
 *
 * @version 1.0
 */
public class PerformanceSort {

    public static void main(String[] args) {
        // 生成成绩文件 学生姓名:分数
//        makeFile();
        // 排序思路：
        // 定义一个对象，包含学生姓名，下一个学生对象引用
        // 定义一个数组，长度0-900
        sortPerformance();

    }

    public static void sortPerformance() {
        try {
            // 数据文件地址
            String fileName = "/Users/zhaobing/temp/performance.txt";
            // 存储年龄数量的数组
            SNode data[] = new SNode[900];
            // 文件中学生数据的总行数
            int tot = 0;
            long start = System.currentTimeMillis();
            InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName), "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            // 时间复杂度 O(n)
            String str = "";
            while ((str = br.readLine()) != null) {
                String info[] = str.split(":");
                String name = info[0];
                int performance = Integer.valueOf(info[1]);
                SNode sNode = new SNode(name);
                // 在对应的下标处增加一个数据节点
                if (data[performance] == null) {
                    data[performance] = sNode;
                } else {
                    sNode.next = data[performance];
                    data[performance] = sNode;
                }
                // 总数量自增1
                tot++;
            }
            System.out.println("学生总人数：" + tot);
            int batch = 0;
            for (int i = data.length - 1; i >= 0; i--) {
                if (null == data[i]) {
                    continue;
                }
                batch++;
                String path = "/Users/zhaobing/temp/第" + batch + "名" + ".txt";
                File file = new File(path);
                if (!file.exists()) {
                    file.createNewFile();
                }
                BufferedWriter out = new BufferedWriter(new FileWriter(path));
                SNode head = data[i];
                while (head != null) {
                    out.write(head.name);
                    out.newLine();
                    head = head.next;
                }
                out.close();
            }
            System.out.println("计算花费的时间为：" + (System.currentTimeMillis() - start) + "ms");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void makeFile() {
        BufferedWriter out = null;
        try {
            int total = 2000000;
            Random random = new Random();
            out = new BufferedWriter(new FileWriter("/Users/zhaobing/temp/performance.txt"));
            for (int i = 0; i < total; i++) {
                int performance = random.nextInt(900);
                String name = "学生" + i;
                out.write(name + ":" + performance);
                out.newLine();
            }

            System.out.println("文件创建成功！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

class SNode {
    String name;
    SNode next;

    public SNode(String name) {
        this.name = name;
    }
}
